﻿using System;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Plumbing;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using YArchitech.Controls;
using YArchitech.HVAC.LevelIndependentTag;
using YArchitech.Revit;

namespace YArchitech.HVAC
{
	public class SlopeIndependent : IndependentBase
	{
		public SlopeIndependent(XArrowJig jig, FrmSlopeIndependent frm, Autodesk.Revit.DB.Document doc, Selection sel, UIApplication uiApp, UIDocument uiDoc) : base(doc, sel, uiApp, uiDoc)
		{
			this.jig = jig;
			this.frm = frm;
		}

		public void SetJigAndLayTag()
		{
			this.frm.SlopeIndependentEvent += this.Frm_SlopeIndependentEvent;
			Reference reference = this.Sel.PickObject((Autodesk.Revit.UI.Selection.ObjectType)1, new PipeSelectionFilter(), "请点选需要标注管道");
			YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(this.Doc.GetElement(reference) as Pipe);
			this.hypipe = hypiping;
			Parameter parameter = this.Doc.GetElement(reference).GetParameter(BuiltInParameter.RBS_PIPE_SLOPE);
			using (this.jig = new XArrowJig(this.UiDoc, reference.GlobalPoint, this.getEndXYZ(hypiping), this.frm.cboSlope.SelectedItem != null && this.frm.cboSlope.SelectedItem.ToString() == "读图" && parameter != null && parameter.HasValue && parameter.AsDouble() != 0.0, hypiping.GetSlopPipeEndPoint()[0] - hypiping.GetSlopPipeEndPoint()[1], this.frm.rbnStream.Checked))
			{
				this.setArrowState();
				XYZ endXYZ;
				do
				{
					endXYZ = this.jig.PickArrowPoint("请选择标注位置:");
				}
				while (!this.IsValid(this.frm.cboSlope.Text));
				if (this.jig.MoveVec.GetLength() == 0.0 && this.frm.rbnStream.Checked && (this.frm.cboSlope.SelectedItem == null || !(this.frm.cboSlope.SelectedItem.ToString() == "读图") || parameter == null || !parameter.HasValue || parameter.AsDouble() == 0.0))
				{
					this.dealVerticalPipe(endXYZ, hypiping, reference);
				}
				ElementId id = this.getCurrentFs(hypiping.Piping).Id;
				IndependentTag independentTag;
				if (this.jig.JigStage == XArrowJig.EStage.eAllArrowPtOnLine || this.jig.JigStage == XArrowJig.EStage.eHaltArrowPtOnLine)
				{
					independentTag = RevitVersionFuncs.NewTag(this.Doc, this.Doc.ActiveView, new Reference(hypiping.Piping), false, 0, 0, reference.GlobalPoint);
				}
				else
				{
					independentTag = RevitVersionFuncs.NewTag(this.Doc, this.Doc.ActiveView, new Reference(hypiping.Piping), false, 0, 0, reference.GlobalPoint + this.jig.MoveVec);
				}
				this.setSlopeValueStyle(hypiping.Piping);
				independentTag.ChangeTypeId(id);
				AssociationMarkInfo.WriteMarkInfoToElement(independentTag, MarkType.Slope);
				this.Doc.Regenerate();
			}
		}

		private void setSlopeValueStyle(Pipe pipe)
		{
			pipe.GetParameter("坡度");
			this.SetPipeSlopValue(pipe);
			if (this.frm.cboSlope.SelectedItem != null && this.frm.cboSlope.SelectedItem.ToString() == "读图")
			{
				this.setSlopeValue();
				return;
			}
			Parameter parameter = pipe.GetParameter("管道坡度");
			if (this.frm.cboSlope.SelectedItem != null)
			{
				parameter.Set(this.getSlopeValue(this.frm.cboSlope.SelectedItem.ToString()));
				return;
			}
			parameter.Set(this.getSlopeValue(this.frm.cboSlope.Text));
		}

		private void SetPipeSlopValue(Pipe pipe)
		{
			Parameter parameter = pipe.GetParameter("坡度");
			Parameter parameter2 = pipe.GetParameter("管道坡度");
			double num = Math.Round(parameter.AsDouble(), 3);
			if (this.frm.markSetting.SlopeStyle == "0.001")
			{
				parameter2.Set(num.ToString("0.000"));
				return;
			}
			if (this.frm.markSetting.SlopeStyle == "%")
			{
				parameter2.Set((num * 100.0).ToString("0.00") + "%");
				return;
			}
			if (this.frm.markSetting.SlopeStyle == "‰")
			{
				string str = (num * 1000.0).ToString("0.00");
				parameter2.Set(str + "‰");
			}
		}

		private bool IsValid(string text)
		{
			if (text == "读图")
			{
				return true;
			}
			bool flag = true;
			try
			{
				flag = (double.Parse(text) >= 0.0);
			}
			catch (Exception)
			{
				YJKMessageBox.Information("请输入合法坡度！");
				this.frm.cboSlope.Focus();
				return false;
			}
			if (!flag)
			{
				YJKMessageBox.Information("请输入合法坡度！");
				this.frm.cboSlope.Focus();
			}
			return flag;
		}

		private void setSlopeValue()
		{
			if (this.frm.markSetting.SlopeStyle == "0.001")
			{
				Units units = this.Doc.GetUnits();
                units.SetFormatOptions((UnitType)71, new FormatOptions((DisplayUnitType)235, 23501)
				{
					UseDefault = false,
					Accuracy = 0.01,
					SuppressTrailingZeros = false,
					SuppressLeadingZeros = false,
					UsePlusPrefix = false,
					UseDigitGrouping = false,
					SuppressSpaces = false
				});
				this.Doc.SetUnits(units);
				return;
			}
			if (this.frm.markSetting.SlopeStyle == "‰")
			{
				Units units2 = this.Doc.GetUnits();
                units2.SetFormatOptions((UnitType)(71), new FormatOptions((DisplayUnitType)(235), 23501)
				{
					UseDefault = false,
					Accuracy = 0.01,
					SuppressTrailingZeros = false,
					SuppressLeadingZeros = false,
					UsePlusPrefix = false,
					UseDigitGrouping = false,
					SuppressSpaces = false
				});
				this.Doc.SetUnits(units2);
				return;
			}
			if (this.frm.markSetting.SlopeStyle == "%")
			{
				Units units3 = this.Doc.GetUnits();
                units3.SetFormatOptions((UnitType)71, new FormatOptions((DisplayUnitType)19, 1901)
				{
					UseDefault = false,
					Accuracy = 0.01,
					SuppressTrailingZeros = false,
					SuppressLeadingZeros = false,
					UsePlusPrefix = false,
					UseDigitGrouping = false,
					SuppressSpaces = false
				});
				this.Doc.SetUnits(units3);
			}
		}

		private string getSlopeValue(string value)
		{
			if (this.frm.markSetting.SlopeStyle == "0.001")
			{
				return value;
			}
			if (this.frm.markSetting.SlopeStyle == "%")
			{
				return (Convert.ToDouble(value) * 100.0).ToString() + "%";
			}
			if (this.frm.markSetting.SlopeStyle == "‰")
			{
				return (Convert.ToDouble(value) * 1000.0).ToString() + "‰";
			}
			return value;
		}

		private void dealVerticalPipe(XYZ endXYZ, YJKPiping<Pipe> HYpipe, Reference refe)
		{
			LocationCurve locationCurve = HYpipe.Piping.Location as LocationCurve;
			XYZ xyzpoint = locationCurve.Curve.Project(endXYZ).XYZPoint;
			XYZ xyz = YJKRevitTools.GetVectorByHorizontal((locationCurve.Curve.GetEndPoint(1) - locationCurve.Curve.GetEndPoint(0)).Normalize());
			if (Math.Abs((endXYZ - new XYZ(xyzpoint.X, xyzpoint.Y, 0.0)).Normalize().X - xyz.X) >= 0.0001 || Math.Abs((endXYZ - new XYZ(xyzpoint.X, xyzpoint.Y, 0.0)).Normalize().Y - xyz.Y) >= 0.0001)
			{
				xyz = -1.0 * xyz;
			}
			this.jig.MoveVec = xyz * this.jig.verticalNum;
			if (this.jig.MoveVec.Y == 0.0)
			{
				if ((endXYZ - refe.GlobalPoint).Y > 0.0)
				{
					this.jig.IsLeft = false;
					return;
				}
				this.jig.IsLeft = true;
				return;
			}
			else
			{
				if ((endXYZ - refe.GlobalPoint).X > 0.0)
				{
					this.jig.IsLeft = false;
					return;
				}
				this.jig.IsLeft = true;
				return;
			}
		}

		private XYZ getEndXYZ(YJKPiping<Pipe> HYpipe)
		{
			if (Math.Abs(HYpipe.EndConnector.Origin.X - HYpipe.StartConnector.Origin.X) > 0.001)
			{
				if (HYpipe.EndConnector.Origin.X <= HYpipe.StartConnector.Origin.X)
				{
					return HYpipe.StartConnector.Origin;
				}
				return HYpipe.EndConnector.Origin;
			}
			else
			{
				if (HYpipe.EndConnector.Origin.Y <= HYpipe.StartConnector.Origin.Y)
				{
					return HYpipe.StartConnector.Origin;
				}
				return HYpipe.EndConnector.Origin;
			}
		}

		private FamilySymbol getCurrentFs(Pipe pipe)
		{
			LevelIndependentTagOperate tagOperate = new LevelIndependentTagOperate(this.Doc);
			FamilySymbol result;
			if (this.frm.rbnStream.Checked)
			{
				result = this.GetStreamFs(tagOperate);
			}
			else
			{
				result = this.GetSlopeFs(tagOperate);
			}
			return result;
		}

		private FamilySymbol GetStreamFs(LevelIndependentTagOperate tagOperate)
		{
			string familyName = "";
			string text = "";
			if (this.frm.markSetting.Style == "全箭头")
			{
				familyName = "HY_水管水流方向1.rfa";
				if (this.jig.IsLeft)
				{
					text = "箭头左";
				}
				else
				{
					text = "箭头右";
				}
			}
			else if (this.frm.markSetting.Style == "半箭头")
			{
				familyName = "HY_水管水流方向2.rfa";
				if (this.jig.IsVertical)
				{
					if (this.jig.MoveVec.X > 0.0)
					{
						text += "箭头上";
					}
					else
					{
						text += "箭头下";
					}
					if (this.jig.IsLeft)
					{
						text += "左";
					}
					else
					{
						text += "右";
					}
				}
				else
				{
					if (this.jig.MoveVec.Y > 0.0)
					{
						text += "箭头上";
					}
					else
					{
						text += "箭头下";
					}
					if (this.jig.IsLeft)
					{
						text += "左";
					}
					else
					{
						text += "右";
					}
				}
			}
			FamilySymbol familySymbolyByName = tagOperate.GetFamilySymbolyByName("MarkTag", familyName, text);
			this.Doc.Regenerate();
			return familySymbolyByName;
		}

		private string GetFamilyName()
		{
			string result = "";
			if (this.frm.cboSlope.SelectedItem != null && this.frm.cboSlope.SelectedItem.ToString() == "读图")
			{
				if (this.frm.markSetting.SlopeStyle == "0.001")
				{
					result = "HY_水管坡度_十进制.rfa";
				}
				else if (this.frm.markSetting.SlopeStyle == "‰")
				{
					result = "HY_水管坡度_千分率.rfa";
				}
				else if (this.frm.markSetting.SlopeStyle == "%")
				{
					result = "HY_水管坡度_百分比.rfa";
				}
			}
			else
			{
				result = "HY_水管坡度_十进制.rfa";
			}
			return result;
		}

		private string GetSymbolName()
		{
			string text = "";
			if (this.jig.IsVertical)
			{
				if (this.jig.MoveVec.X > 0.0)
				{
					text += "下部坡度";
				}
				else
				{
					text += "上部坡度";
				}
				if (this.frm.markSetting.Style == "全箭头")
				{
					text += "全";
				}
				else
				{
					text += "半";
				}
				if (this.jig.IsLeft)
				{
					text += "箭头左";
				}
				else
				{
					text += "箭头右";
				}
			}
			else
			{
				if (this.jig.MoveVec.Y > 0.0)
				{
					text += "上部坡度";
				}
				else
				{
					text += "下部坡度";
				}
				if (this.frm.markSetting.Style == "全箭头")
				{
					text += "全";
				}
				else
				{
					text += "半";
				}
				if (this.jig.IsLeft)
				{
					text += "箭头左";
				}
				else
				{
					text += "箭头右";
				}
			}
			return text;
		}

		private FamilySymbol GetSlopeFs(LevelIndependentTagOperate tagOperate)
		{
			string familyName = this.GetFamilyName();
			string symbolName = this.GetSymbolName();
			FamilySymbol familySymbolyByName = tagOperate.GetFamilySymbolyByName("MarkTag", familyName, symbolName);
			this.Doc.Regenerate();
			return familySymbolyByName;
		}

		private void setArrowState()
		{
			if (this.jig == null)
			{
				return;
			}
			if (this.frm.markSetting.Style == "全箭头")
			{
				if (this.frm.rbnInLine.Checked)
				{
					this.jig.JigStage = XArrowJig.EStage.eAllArrowPtOnLine;
				}
				else
				{
					this.jig.JigStage = XArrowJig.EStage.eAllArrowPtOffLine;
				}
			}
			else if (this.frm.rbnInLine.Checked)
			{
				this.jig.JigStage = XArrowJig.EStage.eHaltArrowPtOnLine;
			}
			else
			{
				this.jig.JigStage = XArrowJig.EStage.eHalfArrowPtOffLine;
			}
			this.jig.isReadProject = (this.frm.cboSlope.SelectedItem != null && this.frm.cboSlope.SelectedItem.ToString() == "读图");
			this.jig.direction = this.hypipe.GetSlopPipeEndPoint()[0] - this.hypipe.GetSlopPipeEndPoint()[1];
		}

		private void Frm_SlopeIndependentEvent(object sender, EventArgs e)
		{
			this.setArrowState();
		}

		private FrmSlopeIndependent frm;

		private XArrowJig jig;

		private YJKPiping<Pipe> hypipe;
	}
}
